package First;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author 长风无言
 */
public class StudentTest {

    public static ObjectInputStream objectInput;
    public static ObjectOutputStream objectOutput;

    static void readFile(StudentFunctionImplements list,List<Student> students) {
        try {
            //创建ObjectOutputStream类关联文件
            objectInput = new ObjectInputStream(new FileInputStream("listStudents.txt"));
            //读取其中数据
            Object obj = objectInput.readObject();
            //强制转换数据类型
            students = (List<Student>) obj;
            //调用构造方法将数据传进list集合
            list.setStudents(students);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //关闭流对象并释放所有资源
            if (null != objectInput) {
                try {
                    objectInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

     static void writeFile(StudentFunctionImplements list,List<Student> students) {
        try {
            objectOutput = new ObjectOutputStream(new FileOutputStream("listStudents.txt"));
            // 写入集合对象
            students = list.getStudents();
            objectOutput.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != objectOutput) {
                try {
                    objectOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {

        StudentFunctionImplements list = new StudentFunctionImplements();
        List<Student> students = new ArrayList<>();

        readFile(list,students);

        Scanner scanner = new Scanner(System.in);

        //用于控制程序是否结束
        boolean flag = true;

        while(flag){
            //打印目录
            System.out.println("------学生信息管理系统------");
            System.out.println("1、增加学生信息");
            System.out.println("2、删除学生信息");
            System.out.println("3、修改学生信息");
            System.out.println("4、查找学生信息");
            System.out.println("5、遍历学生信息");
            System.out.println("0、退出学生信息");

            int choseNum = scanner.nextInt();

            //根据输入的数字进入对应的功能
            switch(choseNum) {
                //增肌学生信息
                case (1):
                    System.out.println("请输入姓名：");
                    String name = scanner.next();
                    System.out.println("请输入学号：");
                    int num = scanner.nextInt();
                    System.out.println("请输入年龄：");
                    int age = scanner.nextInt();
                    try {
                        list.addStudent(name, num, age);
                    } catch (StudentIdException | AgeException e) {
                        e.printStackTrace();
                    }
                    break;
                    //删除学生信息
                case (2):
                    System.out.println("请输入删除学生的学号：");
                    num = scanner.nextInt();
                    try {
                        list.deleteStudents(num);
                    } catch (StudentIdException e) {
                        e.printStackTrace();
                    }
                    break;
                    //根据学号修改学生信息
                case (3):
                    System.out.println("请输入需要修改学生的学号：");
                    num = scanner.nextInt();
                    System.out.println("请输入要修改学生的新信息：");
                    System.out.println("请输入姓名");
                    name = scanner.next();
                    System.out.println("清输入学号");
                    int newnum = scanner.nextInt();
                    System.out.println("请输入年龄");
                    age = scanner.nextInt();
                    try {
                        list.modifyStudents(num, name, newnum, age);
                    } catch (StudentIdException | AgeException e) {
                        e.printStackTrace();
                    }
                    break;
                    //根据学号查询学生信息
                case (4):
                    System.out.println("请入要查找学生的学号：");
                    num = scanner.nextInt();
                    try {
                        list.findStudents(num);
                    } catch (StudentIdException e) {
                        e.printStackTrace();
                    }
                    break;
                    //遍历输出所有学生信息
                case (5):
                    list.forStudent();
                    break;
                    //退出学生系统
                default:
                    System.out.println("退出学生系统");
                    writeFile(list,students);
                    flag = false;
                    break;
            }
        }

    }
}
